#!/bin/bash
username=$1
group=$2
perm=$3
if [[ -z "$username" ]] || [[ -z "$group" ]] || [[ -z "$perm" ]]; then
  echo "add username group perm[0: read, 1: read&write, 2: admin]"
  exit -1
fi

groupadd $group

useradd -g $group -s /sbin/nologin $username

admin=admin
cat <<EOF > /etc/samba/user/$username.smb.conf
[$group]
    comment = $username config
    path = /share/dev
    public = no
    admin users = $group$admin
    valid users = @$group
EOF

if [[ $perm -gt 0 ]]; then
cat <<EOF >> /etc/samba/user/$username.smb.conf
    writable = yes
    create mask = 1777
    directory mask = 1777
    force create mode = 1777
    force directory mode = 1777
EOF

else

cat <<EOF >> /etc/samba/user/$username.smb.conf
    writable = no
EOF

fi

(echo 123456;echo 123456)|smbpasswd -a $username -s